package com.u.tool;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.sql.Blob;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;
import com.u.test.Test;
import com.u.test.test1;

import net.sf.json.JSONArray;

public class SerializeUtil {
	
		public byte[] bytee;

		public static Object Deserialize(Blob blob) throws Exception{
			InputStream is = blob.getBinaryStream();
			BufferedInputStream input = new BufferedInputStream(is);
			byte[] buff = new byte[(int)blob.length()];
			while(-1 != (input.read(buff, 0, buff.length)));
			ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buff));
			Object obj = in.readObject();
			return obj;	
		}
		
		public static byte[] Serialize(Object obj) throws IOException{
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ObjectOutputStream outputStream = new ObjectOutputStream(out);
			outputStream.writeObject(obj);
			byte[] bytes = out.toByteArray();
			outputStream.close();
			return bytes;
		}
		
		public static void main(String[] args) throws IOException, SQLException {
//			MysqlUtil db = new MysqlUtil();
//			Field[] fields = SerializeUtil.class.getDeclaredFields();
//			System.out.println(fields[0].getType().getSimpleName());
			Test test = new Test();
			test1 tt = new test1();
			tt.a = "aaa";
			tt.b = 2222;
			JSONArray json = new JSONArray();
			json.add(tt);
			test.blob = json.toString();
			System.out.println(json);
			MysqlUtil db = new MysqlUtil();
			db.connection();
			db.add(test);
			db.close();
			
			
			
		}
}
